package com.sromku.simple.fb.example.fragments;

import java.util.ArrayList;
import java.util.List;







import com.example.hal.model.Evenement;
import com.example.hal.model.EvenementController;
import com.sromku.simple.fb.example.EvenementAddActivity;
import com.sromku.simple.fb.example.R;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.ListFragment;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;


public class ActualiteFragment extends ListFragment{

private List listsEvenement = null;
private List<String> listsName = null;
private ArrayAdapter<String> evenementArray;
	/*public ActualiteFragment() {
		// TODO Auto-generated constructor stub
		super();
	}

	   @Override 
	    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
	                Bundle savedInstanceState) { 
	    

			setContentView(R.layout.actualite_layout);
		   
	        // Inflate the layout for this fragment 
	        return inflater.inflate( 

	       getResources().getIdentifier("actualite_layout", "layout", container.getContext().getPackageName()), container, false); 
	    }     
	    */
	
	/*@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		TextView textView = new TextView(getActivity());
		textView.setGravity(Gravity.CENTER);
		Bundle args = getArguments();
		textView.setText(args.getString("KEY_STRING"));
		return textView;
	}
	    
	    public static ActualiteFragment newInstance(String chaine) {
	    	ActualiteFragment fragment = new ActualiteFragment();
	    	Bundle args = new Bundle();
	    	args.putString("KEY_STRING", chaine);
	    	fragment.setArguments(args);
	    	return fragment;
	    }*/
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view =inflater.inflate(R.layout.actualite_layout, container, false);
	//	listLists = (ListView)  findListViewById(R.id.listLists);
	//	getAllEvenement();
		
		Button btn = (Button) view.findViewById(R.id.addEvenement);
		btn.setOnClickListener(new OnClickListener() {

		    @Override
		    public void onClick(View v) {
				newEvenement();
		    }
		});
		return view;
		
	}

	
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		
		if(evenementArray==null){
			getAllEvenement();
			
			evenementArray= new ArrayAdapter<String>(getActivity(),	android.R.layout.simple_list_item_1, listsName);
			
			setListAdapter(evenementArray);
		}
	}

  
    
    
	private void getAllEvenement() {

		EvenementController list = new EvenementController();

		listsName  = new ArrayList<String>();
		try {
			listsEvenement = list.getAllEvenement();
		} catch (Exception e) {
			e.printStackTrace();
		}
		if (listsEvenement != null) {
		
			// Log.i("GetAllActiviteActivity",String.valueOf(listsEvenement.size()));
		
			for (Object o : listsEvenement) {
				Evenement u=(Evenement) o;
				if (u != null)
				listsName.add(u.getTitre() + "\n" + u.getTexte());
			}
			
			//listLists.setAdapter(new ArrayAdapter(getBaseContext(),android.R.layout.simple_list_item_1, listsName));
			
			//listLists.setTextFilterEnabled(true);
		
		}
	}
	
	final void newEvenement(){

		 startActivity(new Intent(getActivity(), EvenementAddActivity.class));
	
    }
}
